<?php
class AppController extends Controller {
    var $components = array('Acl', 'Auth', 'Cookie');

    function beforeFilter() {
        // Configure AuthComponent
		$this->Auth->authorize = 'actions';
		// The controller and action responsible for handling user login functions.
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
		// Where users get redirected after logging out.
        $this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'home');
		// Where authenticated users get redirected.
        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index');
		// Allow all actions on pages controller to be publicly accessible.
		$this->Auth->allow(array('display'));
		$this->Auth->actionPath = 'controllers/';
		// Log user in if they have cookie.
		$this->_cookie_login();
		// Needed for charts.
		$this->helpers[] = 'FlashChart';
	}
	
	/**
	 * Helper function to log user in automatically
	 * if a remember me cookie exists.
	 * 
	 * @return 
	 */
	function _cookie_login() {
		// Attempt to read the cookie and see if user has a remember me cookie.
		$cookie = $this->Cookie->read('Auth.User');
		if (!is_null($cookie)) {
			// Cookie is found, try to log user in.
			if ($this->Auth->login($cookie)) {
				// User logged in.
				// Clear auth message, just in case we use it.
				$this->Session->del('Message.auth');
			} else {
				// Failed to log user in.
				// Delete invalid Cookie to be safe.
				$this->Cookie->del('Auth.User');
			}
		}
	}
}
?>
